package com.example.exam.controller;

import com.example.exam.entity.DTO.PaperDTO;
import com.example.exam.service.SimplePaperImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhang
 */
@RestController
@RequestMapping("/paper")
@Log4j2
public class PaperController {
    @Autowired
    private SimplePaperImpl paper;

    /**
     * 添加试卷
     * 根据传入的值查询试题信息，将试题信息添加到试卷中，保存试卷文件
     *
     * @param paperDTO 试卷信息
     * @return 添加成功返回success，失败返回fail
     * @see PaperDTO
     * @see SimplePaperImpl#createPaper
     */
    @PostMapping("/add")
    public ResponseEntity<String> add(@RequestBody PaperDTO paperDTO) {
        return paper.createPaper(paperDTO) == 1 ? ResponseEntity.ok("success") : ResponseEntity.badRequest().body("fail");
    }

    @GetMapping("/{id}")
    public String getPaper(@PathVariable Integer id) {
        return paper.getPaperById(id);
    }

}
